﻿#include <iostream>
#include <memory>
using namespace std;

class Person {
public:
    Person(string name) : name_(name) {
        cout << name_ << " created." << endl;
    }
    ~Person() {
        cout << name_ << " destroyed." << endl;
    }
private:
    string name_;
};

int main() {
    shared_ptr<Person> p1 = make_shared<Person>("Alice");
    {
        shared_ptr<Person> p2 = p1;  // 引用计数 +1
        cout << "Inside block." << endl;
    } // p2 释放，引用计数 -1
    cout << "Back to main." << endl;
    return 0; // p1 释放，引用计数为0，自动析构
}
